﻿using ESC.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ESC.Designer.Resources
{
    public static class Utils
    {
        public static T DeepCopyByReflect<T>(T obj)
        {
            //如果是字符串或值类型则直接返回
            if (obj is string || obj.GetType().IsValueType) return obj;
            object retval = Activator.CreateInstance(obj.GetType());
            FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
            foreach (FieldInfo field in fields)
            {
                try { field.SetValue(retval, DeepCopyByReflect(field.GetValue(obj))); }
                catch { }
            }
            return (T)retval;
        }

        public static bool IsJamiconPart(string partno)
        {
            if (string.IsNullOrEmpty(partno))
                return false;
            if (partno.Length != 18)
                return false;
            using (ESCEntities db = new ESCEntities())
            {
                //第一碼判斷
                string s1 = partno.Substring(0, 1);
                if (db.FQCParams.Count(x => x.code == s1 && x.type == "Jamicon1") == 1)
                {
                    return true;
                }
                //前三碼判斷
                string s3 = partno.Substring(0, 3);
                if (db.FQCParams.Count(x => x.code == s3 && x.type == "Jamicon3") == 1)
                {
                    return true;
                }
            }
            return false;
        }


        public static string GetK(string fk, string partno, string ctype)
        {
            try
            {
                List<string> ls = new List<string> { "J", "K", "L", "6", "M", "N", "7", "O", "P", "Q", "R", "S", "T", "U", "3" };
                if (!ls.Contains(partno.Substring(14, 1)) || partno.Substring(15, 2).CompareTo("25") < 0)
                {
                    return "";
                }
                if (ctype == "POL")
                    return "";
                else if (ctype == "SNP")
                {
                    decimal fkd = Convert.ToDecimal(fk);
                    if (partno.Substring(15, 2).CompareTo("25") < 0)
                    {
                        return fkd.ToString("0.###");
                    }
                    if (partno.Substring(17, 1) == "4" && partno.Substring(6, 1) == "M")
                    {
                        return (fkd - 0.04m).ToString("0.###");
                    }
                    return (fkd - 0.02m).ToString("0.###");
                }
                else
                {
                    decimal fkd = Convert.ToDecimal(fk);
                    //KSH825L400D2S5812K
                    if (partno.Substring(17, 1) == "4")
                    {
                        return (fkd - 0.03m).ToString("0.###");
                    }
                    if (partno.Substring(0, 3) == "DSM" || partno.Substring(0, 3) == "KSM")
                    {
                        if (partno.Substring(6, 1) == "M")
                        {
                            return (fkd - 0.02m).ToString("0.###");
                        }
                        return fkd.ToString("0.###");
                    }
                    List<string> strs = new List<string>() { "SC", "SY", "SJ", "SZ", "ST", "TA", "TB", "TC", "AK", "AH", "LZ", "LT", "LU", "LY", "LW", "SX" };
                    if (strs.Contains(partno.Substring(1, 2)))
                    {
                        if (partno.Substring(6, 1) == "M")
                        {
                            return (fkd - 0.02m).ToString("0.###");
                        }
                        return fkd.ToString("0.###");
                    }
                    if (partno.Substring(6, 1) == "M" || partno.Substring(6, 1) == "G")
                    {
                        return (fkd - 0.03m).ToString("0.###");
                    }
                    if (partno.Substring(6, 1) == "E" || partno.Substring(6, 1) == "L")
                    {
                        return (fkd - 0.02m).ToString("0.###");
                    }
                    if (partno.Substring(7, 3).CompareTo("101") <= 0)
                    {
                        return (fkd - 0.02m).ToString("0.###");
                    }
                    else
                    {
                        return (fkd - 0.04m).ToString("0.###");
                    }
                }
            }
            catch
            {
            }
            return "";
        }
    }
}
